<?php
session_start();
?>

<!DOCTYPE HTML>
<html>

    <head>
        <title>3D|didongde 3D- Di động đây</title>
        <?php include('modules/page_head.php'); ?>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include_once('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include_once('modules/menubar.php'); ?>
                </div>
            </div>
            <div id="site_content">
                <div id="sidebar_container">
                    <h2>Tìm kiếm nhanh</h2>
                    <div class="sidebar">
                        <?php
                        include ("modules/quick_search.php");
                        show_manu();
                        ?>
                    </div>
                    <div class="sidebar">
                        <?php show_price(); ?>
                    </div>
                </div>
                <div id="content">
                    <!-- insert the page content here -->
                    <?php
                    include_once("modules/connection.php");
                    $conn = connectDB();
                    ?>
                    <h2>Tìm kiếm nâng cao</h2>
                    <div >
                        <?php
                        $flag = 0;
                        if ((isset($_GET["submit"])) || (isset($_REQUEST['manu'])) || (isset($_REQUEST['price']))) {
                            $flag = 1;
                            if (isset($_GET['model']))
                                $model = $_GET['model'];
                            else
                                $model = null;
                            echo "<b>Bạn đang tìm kiếm điện thoại:</b><br/><b>+ Hãng: </b>";

                            if (isset($_REQUEST['manu']))
                                $manufacturer = $_REQUEST["manu"];
                            else
                                $manufacturer = -1;
                            if ($manufacturer != -1)
                                echo $manufacturer;
                            else
                                echo "Bất kì";

                            echo "<br/><b>+ Giá tiền: </b>";

                            if (isset($_REQUEST['price']))
                                $price = $_REQUEST["price"];
                            else
                                $price = -1;
                            if ($price == -1)
                                echo "Bất kì";
                            else if ($price == 1000)
                                echo "Dưới 1 triệu đồng";
                            else if ($price == 2000)
                                echo "Từ 1 đến 2 triệu đồng";
                            else if ($price == 5000)
                                echo "Từ 2 đến 5 triệu đồng";
                            else if ($price == 10000)
                                echo "Từ 5 đến 10 triệu đồng";
                            else if ($price == 50000)
                                echo "Từ 10 đến 50 triệu đồng";
                            else if ($price == 50001)
                                echo "Trên 50 triệu đồng";
                            else
                                echo "Không giới hạn";

                            echo "<br /><b>+ Tính năng: </b>";
                            ?>
                            <table><tr>
                                    <th>Camera:</th>
                                    <td style="padding-right:40px">
                                        <?php
                                        if (isset($_REQUEST['camera']))
                                            $camera = $_REQUEST["camera"];
                                        else
                                            $camera = "off";
                                        if ($camera == 'on')
                                            echo "Có";
                                        else
                                            echo "Bất kì";
                                        ?>
                                    </td>
                                    <th>GPRS:</th>
                                    <td style="padding-right:40px">
                                        <?php
                                        if (isset($_REQUEST['GPRS']))
                                            $gprs = $_REQUEST["GPRS"];
                                        else
                                            $gprs = "off";
                                        if ($gprs == "on")
                                            print("Có");
                                        else
                                            echo "Bất kì";
                                        ?></td>
                                    <th>Wi-fi:</th>
                                    <td style="padding-right:40px">
                                        <?php
                                        if (isset($_REQUEST['wifi']))
                                            $wifi = $_REQUEST["wifi"];
                                        else
                                            $wifi = "off";
                                        if ($wifi == 'on')
                                            echo "Có";
                                        else
                                            echo "Bất kì";
                                        ?></td>
                                    <th>Hệ điều hành:</th>
                                    <td style="padding-right:40px">
                                        <?php
                                        if (isset($_REQUEST['OS']))
                                            $OS = $_REQUEST["OS"];
                                        else
                                            $OS = -1;
                                        if ($OS == -1)
                                            echo "Bất kì";
                                        else
                                            echo "$OS";
                                        ?></td>
                                </tr></table>

                            <?php
                            if ($model != null) {
                                echo "<b>Bạn đang tìm điện thoại:</b><br/>";
                                echo "<b>Model:</b> $model<br/><br/>";
                            }

                            /* define queries to search
                             * -------------------------------------------------- */
                            /* query 1: all mobile models from any manufacturer  */
                            $query_1 = "SELECT * FROM mobilephone WHERE 1 = 1 ";
                            /* query 2: all mobile models from $manufacturer  */
                            $query_2 = "select mobilephone.* from mobilephone, provides, manufacturer where (mobilephone.mobileid = provides.mobileid) and (provides.manuid = manufacturer.manuid) and (name = '$manufacturer')";

                            if ($manufacturer == -1)
                                $query = $query_1;
                            else
                                $query = $query_2;

                            if ($price == 1000)
                                $query .= "and (mobilephone.price < 1000)";
                            else if ($price == 2000)
                                $query .= "and (mobilephone.price >= 1000)
					and (mobilephone.price <= 2000)";
                            else if ($price == 5000)
                                $query .= "and (mobilephone.price >= 2000)
					and (mobilephone.price <= 5000)";
                            else if ($price == 10000)
                                $query .= "and (mobilephone.price >= 5000)
					and (mobilephone.price <= 10000)";
                            else if ($price == 50000)
                                $query .= "and (mobilephone.price >= 10000)
					and (mobilephone.price <= 50000)";
                            else if ($price == 50001)
                                $query .= "and (mobilephone.price > 50000)";

                            if ($gprs == 'on')
                                $query .= "and network like '%GPRS%'";
                            if ($camera == 'on')
                                $query .= "and entertainment like '%Máy ảnh%'";
                            if ($wifi == 'on')
                                $query .= "and network like '%Wi-fi%'";

                            if ($OS != -1)
                                $query .= "and (os like '$OS')";

                            if ($model != null) {
                                $query = "select * from mobilephone where upper(model) like '%" . strtoupper($model) . "%'";
                            }
                            ?>

                            <div id="mobile_list">
                                <?php
                                include_once("modules/mobile_list.php");
                                $num = get_result($query);
                                ?>
                            </div>

                            <?php
                            if ($num != 0)
                                echo "<p>Có " . $num . " mẫu điện thoại bạn cần tìm</p>";
                            else
                                echo "<p>Không có mẫu điện thoại bạn cần tìm</p>";
                        }

                        if ($flag == 1)
                            echo "<br/><h3>Tìm điện thoại khác</h3>";
                        ?>
                    </div>
                    <div class="form_settings">
                        <form method="get" action="#">
                            <input name="model" type="text" class="form_settings"
                            <?php
                            if (isset($_GET['model']))
                                echo "value=\"{$_GET['model']}\""; else
                                echo "placeholder=\"Tên điện thoại\"";
                            ?>/><br/><br/>
                            <select id="id" name="manu" style="width:auto;">
                                <?php
                                echo "<option value=\"-1\">Hãng sản xuất</option>";
                                $query_manu = "SELECT distinct name FROM manufacturer";
                                $stm = $conn->prepare($query_manu);
                                $stm->execute();
                                while ($show_manu = $stm->fetch()) {
                                    echo "<option value=\"" . $show_manu['name'] . "\"";
                                    if ((isset($_REQUEST['manu'])) and ($show_manu['name'] == $_REQUEST['manu']))
                                        echo " selected=\"selected\"";
                                    echo ">" . $show_manu['name'] . "</option>";
                                }
                                ?>
                            </select>
                            <select id="id" name="price" style="width:auto;">
                                <option value="-1">Chọn mức giá</option>
                                <option value="1000" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 1000))
                                    echo " selected=\"selected\""; ?>>Dưới 1 triệu</option>
                                <option value="2000" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 2000))
                                            echo " selected=\"selected\""; ?>>Từ 1 ->2 triệu</option>
                                <option value="5000" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 5000))
                                            echo " selected=\"selected\""; ?>>Từ 2 ->5 triệu</option>
                                <option value="10000" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 10000))
                                            echo " selected=\"selected\""; ?>>Từ 5 ->10 triệu</option>
                                <option value="50000" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 50000))
                                            echo " selected=\"selected\""; ?>>Từ 10 -> 50 triệu</option>
                                <option value="50001" <?php if ((isset($_REQUEST['price'])) and ($_REQUEST['price'] == 50001))
                                            echo " selected=\"selected\""; ?>>Trên 50 triệu</option>
                            </select>

                            <select id="id" name= "OS" style="width:auto;">
                                <option value="-1">Hệ điều hành</option>
                                <?php
                                $query_showos = "select distinct os from mobilephone";
                                $stm = $conn->prepare($query_showos);
                                $stm->execute();
                                while ($show_manu = $stm->fetch()) {
                                    echo "<option value=\"" . $show_manu['os'] . "\"";
                                    if ((isset($_REQUEST['os'])) and ($show_manu['os'] == $_REQUEST['os']))
                                        echo " selected=\"selected\"";
                                    echo ">" . $show_manu['os'] . "</option>";
                                }
                                ?>
                            </select>

                            <table>
                                <tr><td><label for="camera">Camera</label></td><td><input type="checkbox" class="checkbox" id="camera" "name="camera" style="padding-right: 40px;" <?php if (isset($_REQUEST['camera']) && ($_REQUEST['camera'] == 'on'))
                                    echo "checked=\"checked\""; ?>/></td>
                                    <td><label for="GPRS">GPRS/EDGE</label></td><td><input type="checkbox" class="checkbox" id="GPRS" name="GPRS" style="padding-right: 40px;" <?php if (isset($_REQUEST['GPRS']) && ($_REQUEST['GPRS'] == 'on'))
                                                                                              echo "checked=\"checked\""; ?>/></td>
                                    <td><label for="wifi">Wifi</label></td><td><input type="checkbox" class="checkbox" id="wifi" name="wifi" style="padding-right: 40px;" <?php if (isset($_REQUEST['wifi']) && ($_REQUEST['wifi'] == 'on'))
                                                                                               echo "checked=\"checked\""; ?>/></td></tr>
                            </table>
                            <button title="Tiến hành tìm kiếm" class="submitIMG" name="compare" type="submit" value="search" onmouseover="changepic(this)" onmouseout="returnpic(this)"><img class="submitIMG" alt="Tìm kiếm" src="style/images/search-button.png"/></button>
                        </form>
                    </div>
                    <br>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include('modules/footer.php'); ?>
        </div>
    </body>
</html>
